import os
import requests
import codecs
import tarfile
import gc

def check_dir(path):
    if os.path.exists(path):
        return True
    else:
        try:
            os.makedirs(path, exist_ok= True)
            return True
        except Exception as e:
            print(e)
            return False
        finally:
            return False

def download_data(url):
    data_file_name = url.split("/")[-1]
    path = os.path.join("./data", data_file_name)
    if os.path.exists(path):
        return path
    with codecs.open(path, "wb") as f:
        req = requests.get(url)
        f.write(req.content)
        req.close()
    return path

def extract_tar_file(file, path= None):
    tar = tarfile.open(file)
    dest_path = path
    if dest_path is None:
        dest_path = "/".join(file.split("\\")[:-1])
    if os.path.exists(dest_path):
        return True
    tar.extractall(dest_path)

def run():
    data_path = "./data"
    data_url  = "https://linqs-data.soe.ucsc.edu/public/lbc/cora.tgz"
    check_dir(data_path)
    data_path = download_data(data_url)
    print(data_path)
    extract_tar_file(data_path)

def get_index_dict(iterator) -> dict:
    iter_set = set(iterator)
    temp = {v:k for k,v in enumerate(iter_set)}
    del iter_set
    gc.collect()
    return temp

if __name__ == "__main__":
    run()